<?php
/**
 * Created by 火蝶.
 * User: 蚂蚁
 * Date: 2017/4/15
 * Time: 9:21
 */
global $_W, $_GPC;
$op = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
if ($op == 'display') {

    $list = pdo_fetchall("select * from" . tablename('tg_saler') . "where uniacid='{$_W['uniacid']}'");
    foreach ($list as $key => $value) {
        $storeid_arr = explode(',', $value['storeid']);
        $storename = '';
        foreach ($storeid_arr as $k => $v) {
            if ($v) {
                $store = pdo_fetch("select * from" . tablename('tg_store') . "where id='{$v}'");
                $storename .= $store['storename'] . "/";
            }
        }
        $storename = substr($storename, 0, strlen($storename) - 1);
        $list[$key]['storename'] = $storename;
        $list[$key]['merchant'] = pdo_fetch("SELECT * FROM " . tablename('tg_merchant') . " WHERE uniacid = '{$_W['uniacid']}' and id={$value['merchantid']}");
    }
} elseif ($op == 'post') {
    $id = $_GPC['id'];

    if (!empty($id)) {
        $saler = pdo_fetch("select * from" . tablename('tg_saler') . "where uniacid='{$_W['uniacid']}' and id = '{$id}'");

        $piclist = array();
        $img_arr = explode(',', $saler['img']);
        foreach ($img_arr as $k => $v) {
            $piclist[] = $v;
        }
        $storeid_arr = explode(',', $saler['storeid']);
        $storename = '';
        foreach ($storeid_arr as $k => $v) {
            if ($v) {
                $stores[$k] = pdo_fetch("select * from" . tablename('tg_store') . "where id='{$v}' and uniacid='{$_W['uniacid']}'");
            }
        }
        $xiangmus = pdo_fetchall('select * from ' . tablename('tg_openid_xiangmu') . ' where uniacid=:uniacid and openid=:openid', array(':openid' => $saler['openid'], ':uniacid' => $_W['uniacid']));
        foreach ($xiangmus as $k => &$v) {
            $goods = pdo_fetch('select * from ' . tablename(tg_goods) . ' where uniacid=:uniacid and id=:id', array(':uniacid' => $v['uniacid'], ':id' => $v['goods_id']));
            $v['goodsname'] = $goods['gname'];
        }
    }
    if (checksubmit('salersubmit')) {
        wl_load()->model('member');
        $id = $_GPC['id'];
        $str = '';
        $storeids = $_GPC['storeids'];
        $xiangmuids = $_GPC['xiangmuids'];
        if ($storeids) {
            foreach ($storeids as $key => $value) {
                if ($value) {
                    $str .= $value . ",";
                }
            }
        }
        $str = substr($str, 0, strlen($str) - 1);
        $data = array(
            'uniacid' => $_W['uniacid'],
            'openid' => $_GPC['openid'],
            'sex' => $_GPC['sex'],
            'kefu' => $_GPC['kefu'],
            'mobile' => $_GPC['mobile'],
            'storeid' => $str,
            'status' => $_GPC['salerstatus'],
            'merchantid' => $_GPC['merchantid'],
            'content' => $_GPC['content'],
            'open' => $_GPC['open'],
            'uname' => $_GPC['uname'],
            'password' => $_GPC['password']
        );


        // message(array_reverse($_GPC['img']));
        $goimages = array_reverse($_GPC['img']);
        $s = "";
        foreach ($goimages as $key => $value) {
            if (!empty($s)) {
                $s .= "," . tomedia($goimages[$key]);
            } else {
                $s .= tomedia($goimages[$key]);
            }

        }
        $data['img'] = $s;


        if ($data['openid'] == '') {
            message('必须选择技师！', referer(), 'error');
            exit;
        }
        $info = member_get_by_params(" openid = '{$data['openid']}'");
        $data['avatar'] = $info['avatar'];
        $data['nickname'] = $info['nickname'];

        $data['uniacid'] = $_W['uniacid'];
        $data['createtime'] = TIMESTAMP;

        if ($id) {
            pdo_update('tg_saler', $data, array('id' => $id));
        } else {
            pdo_insert('tg_saler', $data);
            $id = pdo_insertid();
        }

        if ($_W['uniacid'] == 5) {
            if ($xiangmuids) {
                pdo_delete('tg_openid_xiangmu', array('uniacid' => $_W['uniacid'], 'openid' => $_GPC['openid']));
                foreach ($xiangmuids as $key => $value) {
                    $data_xiangmu = array();
                    $data_xiangmu['uniacid'] = $_W['uniacid'];
                    $data_xiangmu['openid'] = $_GPC['openid'];
                    $data_xiangmu['goods_id'] = $value;
                    $data_xiangmu['store_id'] = $id;
                    pdo_insert('tg_openid_xiangmu', $data_xiangmu);
                }
            }
        }else{
            pdo_delete('tg_openid_xiangmu', array('uniacid' => $_W['uniacid'], 'openid' => $_GPC['openid']));
            if ($xiangmuids) {
                foreach ($xiangmuids as $key => $value) {
                    $data_xiangmu = array();
                    $data_xiangmu['uniacid'] = $_W['uniacid'];
                    $data_xiangmu['openid'] = $_GPC['openid'];
                    $data_xiangmu['goods_id'] = $value;
                    $data_xiangmu['store_id'] = $id;
                    pdo_insert('tg_openid_xiangmu', $data_xiangmu);
                }
            }
        }
        message('操作成功！', web_url('goods/saler'), 'success');
    }
} elseif ($op == 'delete') {
    $id = $_GPC['id'];
    pdo_delete('tg_saler', array(
        'id' => $id
    ));
    message('删除成功！', referer(), 'success');
}
include wl_template('goods/saler');
exit();
